﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MS_Enitity;
using System.Threading;
using System.IO;
using xktNodeSettings.Node.Variable;
using xktComm;
using xktNodeSettings;
using xktComm.Common;
using xktComm.DataConvert;

namespace MS_UI
{
    public partial class Frmkeyboard : Form
    {
        private const int WS_EX_TOOLWINDOW = 0x00000080;
        private const int WS_EX_NOACTIVATE = 0x08000000;
        //  重写该方法实现窗体变为浮动工具条，防止闪屏
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                cp.Parent = IntPtr.Zero;
                return cp;
            }
        }

        #region 定义变量
        public string InValue = null;
        public string OutValue = null;
        public string MinValue = null;
        public string MaxValue = null;
        public string PointNo = null;
        public int b = 8; //整数位数；



        private MelsecVariable var;
        private bool PLCWriteFlag;

        #endregion

        public Frmkeyboard(string VarName)
        {
            InitializeComponent();

            //通过变量名称获取到对象
            var = CommMethods.CurrentMelsecVarList[VarName];

            this.InValue = var.Value.ToString();
            this.MinValue = var.Config.SetLimitMin.ToString();
            this.MaxValue = var.Config.SetLimitMax.ToString();

            switch (var.Scale)
            {
                case "0":
                    this.PointNo = "0";
                    break;
                case "0.1":
                    this.PointNo = "1";
                    break;
                case "0.01":
                    this.PointNo = "2";
                    break;
                case "0.001":
                    this.PointNo = "3";
                    break;

                default:
                    break;
            }
        }



        public Frmkeyboard(string invalue, string MinValue, string MaxValue, string PointNO)
        {
            InitializeComponent();
            this.InValue = invalue;
            this.MinValue = MinValue;
            this.MaxValue = MaxValue;
            this.PointNo = PointNO;
            this.PLCWriteFlag = true;

        }



        private void Frmkeyboard_Load(object sender, EventArgs e)
        {
            this.TXT_InValue.Text = InValue;
            this.Lbl_Min.Text = MinValue;
            this.Lbl_Max.Text = MaxValue;
            this.lbl_points.Text = PointNo;
            TXT_InValue.Select();
        }


        #region 用按钮控件实现键盘按键的功能


        private void Btn_CLR_Click(object sender, EventArgs e)
        {
            this.TXT_InValue.Clear();

        }
        private void Btn_ESC_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Btn_ENT_Click(object sender, EventArgs e)
        {
            if (PLCWriteFlag)
            {
                OutValue = this.TXT_InValue.Text.Trim();
                float Invalue;
                if (float.TryParse(OutValue, out Invalue))
                {
                    float Minvalue = float.Parse(MinValue);
                    float Maxvalue = float.Parse(MaxValue);
                    if (Invalue >= Minvalue && Invalue <= Maxvalue)
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                }
            }
            else
            {
                XktResult<string> result = VerifyInputValue(var.Config, this.TXT_InValue.Text.Trim(), var.VarType, var.Scale, var.Offset);

                if (result.IsSuccess)
                {
                    if (CommMethods.CommonMelsecWrite(var, result.Content))
                    {
                        // MessageBox.Show("写入成功");
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("写入失败");
                    }
                }
                else
                {
                    MessageBox.Show(result.Message, "参数错误");
                    return;
                }
            }

        }

        #endregion

        private XktResult<string> VerifyInputValue(ConfigEnitity alarm, string set, DataType type, string scale, string offset)
        {
            XktResult<string> result = new XktResult<string>();
            //非空
            if (set.Length == 0)
            {
                result.IsSuccess = false;
                result.Message = "参数错误：设定值不能为空";
                return result;
            }

            //设定限值
            if (alarm.SetLimitEnable)
            {
                float value = 0.0f;

                if (float.TryParse(set, out value))
                {
                    if (value > alarm.SetLimitMax || value < alarm.SetLimitMin)
                    {
                        result.IsSuccess = false;
                        result.Message = "参数错误：不在设定范围内";
                        return result;
                    }
                }
                else
                {
                    result.IsSuccess = false;
                    result.Message = "参数错误：设定值格式不正确";
                    return result;
                }
            }
            //转换验证

            return MigrationLib.SetMigrationValue(set, type, scale, offset);
        }


        #region 对输入内容进行验证，过滤非法输入
        private void TXT_InValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            int a = Convert.ToInt16(PointNo);
            if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && e.KeyChar != 46)
            {
                e.Handled = true;
                return;
            }
            if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0)) e.Handled = true;
            if (e.KeyChar == 46 && ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true;

            if (a <= 0)
            {
                if ((int)e.KeyChar == 46)
                {
                    e.Handled = true;
                    return;
                }
                if (((TextBox)sender).Text.Trim().Length >= b)
                {
                    ((TextBox)sender).Text = ((TextBox)sender).Text.Trim().Remove(((TextBox)sender).Text.Length - 1, 1);
                    ((TextBox)sender).Select(((TextBox)sender).Text.Length, 0);
                }
            }

            else
            {

                if (((TextBox)sender).Text.Trim().IndexOf('.') < 0)
                {
                    if (((TextBox)sender).Text.Trim().Length >= b && (int)e.KeyChar != 46)
                    {
                        ((TextBox)sender).Text = ((TextBox)sender).Text.Trim().Remove(((TextBox)sender).Text.Length - 1, 1);
                        ((TextBox)sender).Select(((TextBox)sender).Text.Length, 0);
                    }
                }
                else
                {
                    string str = ((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1);
                    if (str.Length >= a)
                    {
                        ((TextBox)sender).Text = ((TextBox)sender).Text.Trim().Remove(((TextBox)sender).Text.Length - 1, 1);
                        ((TextBox)sender).Select(((TextBox)sender).Text.Length, 0);
                    }
                }
            }
        }

        #endregion

        private void TXT_InValue_KeyDown(object sender, KeyEventArgs e)
        {


        }




        private void Btn_MouseUp(object sender, MouseEventArgs e)
        {
            ((Button)sender).BackColor = Color.FromArgb(224, 224, 224);
        }

        private void Btn_1_MouseDown(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send("1");
            Btn_1.BackColor = Color.Lime;
        }

        private void Btn2_MouseDown(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send("2");
            Btn_2.BackColor = Color.Lime;
        }

        private void Btn3_MouseDown(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send("3");
            Btn_3.BackColor = Color.Lime;
        }

        private void Btnbs_MouseDown(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send("{BS}");
            Btn_bs.BackColor = Color.Lime;
        }

        private void Btn4_MouseDown(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send("4");
            Btn_4.BackColor = Color.Lime;
        }

        private void Btn5_MouseDown(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send("5");
            Btn_5.BackColor = Color.Lime;
        }

        private void Btn6_MouseDown(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send("6");
            Btn_6.BackColor = Color.Lime;
        }

        private void Btnl_MouseDown(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send("{LEFT}");
            Btn_L.BackColor = Color.Lime;
        }

        private void Btnr_MouseDown(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send("{RIGHT}");
            Btn_R.BackColor = Color.Lime;
        }

        private void Btn7_MouseDown(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send("7");
            Btn_7.BackColor = Color.Lime;
        }

        private void Btn8_MouseDown(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send("8");
            Btn_8.BackColor = Color.Lime;
        }

        private void Btn9_MouseUp(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send("9");
            Btn_9.BackColor = Color.Lime;
        }

        private void Btn0_MouseDown(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send("0");
            Btn_0.BackColor = Color.Lime;
        }

        private void Btnp_MouseDown(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send(".");
            Btn_point.BackColor = Color.Lime;
        }

        private void Btnfu_MouseDown(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send("{-}");
            Btn_fu.BackColor = Color.Lime;
        }

        private void Btn9_MouseDown(object sender, MouseEventArgs e)
        {
            this.TXT_InValue.Focus();
            SendKeys.Send("9");
            Btn_9.BackColor = Color.Lime;
        }

        private void BtnESC_MouseDown(object sender, MouseEventArgs e)
        {
            Btn_ESC.BackColor = Color.Lime;
        }

        private void BtnENT_MouseDown(object sender, MouseEventArgs e)
        {
            Btn_ENT.BackColor = Color.Lime;
        }

        private void BtnCLR_MouseDown(object sender, MouseEventArgs e)
        {
            Btn_CLR.BackColor = Color.Lime;
        }

        private void Btn_1_MouseLeave(object sender, EventArgs e)
        {
            Btn_1.BackColor = Color.FromArgb(224, 224, 224);
        }
    }
}
